<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Set the value of an SNMP object</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.snmp2-real-walk.html">snmp2_real_walk</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.snmp2-walk.html">snmp2_walk</a></div>
 <div class="up"><a href="ref.snmp.html">SNMP 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.snmp2-set" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">snmp2_set</h1>
  <p class="verinfo">(PHP &gt;= 5.2.0)</p><p class="refpurpose"><span class="refname">snmp2_set</span> &mdash; <span class="dc-title">Set the value of an <acronym title="Simple Network Management Protocol">SNMP</acronym> object</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.snmp2-set-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>snmp2_set</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$host</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$community</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$object_id</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$type</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$value</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$timeout</code><span class="initializer"> = 1000000</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$retries</code><span class="initializer"> = 5</span></span>
  ]] )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>snmp2_set()</strong></span> is used to set the value of an <acronym title="Simple Network Management Protocol">SNMP</acronym> object
   specified by the <em><code class="parameter">object_id</code></em>.
  </p>
  
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.snmp2-set-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<em><code class="parameter">host</code></em></dt>

    <dd>

     <p class="para">
      The hostname of the <acronym title="Simple Network Management Protocol">SNMP</acronym> agent (server).
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">community</code></em></dt>

    <dd>

     <p class="para">
      The write community.
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">object_id</code></em></dt>

    <dd>

     <p class="para">
      The <acronym title="Simple Network Management Protocol">SNMP</acronym> object id.
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">type</code></em></dt>

    <dd>

     <p class="para">
<acronym>MIB</acronym> 定义了各个对象 id 的类型。必须是下面列出的单个字符之一。
</p>
<table class="doctable table">
 <caption><strong>类型</strong></caption>
 
  <tbody class="tbody">
   <tr><td>=</td><td>MIB类型</td></tr>

   <tr><td>i</td><td>INTEGER</td> </tr>

   <tr><td>u</td><td>INTEGER</td></tr>

   <tr><td>s</td><td>STRING</td></tr>

   <tr><td>x</td><td>HEX STRING</td></tr>

   <tr><td>d</td><td>DECIMAL STRING</td></tr>

   <tr><td>n</td><td>NULLOBJ</td></tr>

   <tr><td>o</td><td>OBJID</td></tr>

   <tr><td>t</td><td>TIMETICKS</td></tr>

   <tr><td>a</td><td>IPADDRESS</td></tr>

   <tr><td>b</td><td>BITS</td></tr>

  </tbody>
 
</table>

<p class="para">
 如果在编译 <acronym title="Simple Network Management Protocol">SNMP</acronym> 库时定义了 <strong><code>OPAQUE_SPECIAL_TYPES</code></strong>，那么下列值是合法的：
</p>
<table class="doctable table">
 <caption><strong>types</strong></caption>
 
  <tbody class="tbody">
   <tr><td>U</td><td>unsigned int64</td></tr>

   <tr><td>I</td><td>signed int64</td></tr>

   <tr><td>F</td><td>float</td></tr>

   <tr><td>D</td><td>double</td></tr>

  </tbody>
 
</table>


     <p class="para">
这些值大都会使用与 ASN.1 相符的类型。&#039;s&#039;，&#039;x&#039;，&#039;d&#039;
以及 &#039;b&#039; 都是指定一个八字节字符串值的方式。并且 &#039;u&#039; 无符号类型也可用于处理 Gauge32 值。
</p>

     <p class="para">
如果 MIB 文件是用 &quot;snmp_read_mib&quot; 或者通过在 libsnmp config 中指定而加载入 MIB 树时，
&#039;=&#039; 可以被用作为所有对象的 <em><code class="parameter">type</code></em> 参数，因为 type 可以被自动从 MIB 中读取。
</p>

     <p class="para">
注意有两种方式可以设定 BITS 类型的变量，例如
&quot;SYNTAX    BITS {telnet(0), ftp(1), http(2), icmp(3), snmp(4), ssh(5), https(6)}&quot;:
</p>
<ul class="itemizedlist">
 <li class="listitem">
  <span class="simpara">
   使用 &quot;b&quot; 类型以及一个位数的列表。不推荐使用此方法，因为 GET 查询对同一个 OID 将会返回例如 0xF8。
  </span>
 </li>
 <li class="listitem">
  <span class="simpara">
   使用 &quot;x&quot; 类型以及一个十六进制数但是不需要通常的 &quot;0x&quot; 前缀。
  </span>
 </li>
</ul>
<p class="para">
 更多细节见范例部分。
</p>

    </dd>

   
   
    <dt>
<em><code class="parameter">value</code></em></dt>

    <dd>

     <p class="para">
      The new value.
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">timeout</code></em></dt>

    <dd>

     <p class="para">
      The number of microseconds until the first timeout.
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">retries</code></em></dt>

    <dd>

     <p class="para">
      The number of times to retry if timeouts occur.
     </p>
    </dd>

   
  </dl>

 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.snmp2-set-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
  <p class="para">
   If the SNMP host rejects the data type, an E_WARNING message like &quot;Warning: Error in packet. Reason: (badValue) The value given has the wrong type or length.&quot; is shown.
   If an unknown or invalid OID is specified the warning probably reads &quot;Could not add variable&quot;.
  </p>
 </div>

 
 
 
 <div class="refsect1 examples" id="refsect1-function.snmp2-set-examples">
  <h3 class="title">范例</h3>
   <div class="example" id="example-4464">
    <p><strong>Example #1 Using <span class="function"><strong>snmp2_set()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;snmp2_set</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"public"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"IF-MIB::ifAlias.3"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"s"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-4465">
    <p><strong>Example #2 Using <span class="function"><strong>snmp2_set()</strong></span> for setting BITS <acronym title="Simple Network Management Protocol">SNMP</acronym> object id</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;snmp2_set</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"public"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'FOO-MIB::bar.42'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'0&nbsp;1&nbsp;2&nbsp;3&nbsp;4'</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;or<br />&nbsp;&nbsp;</span><span style="color: #0000BB">snmp2_set</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"public"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'FOO-MIB::bar.42'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'x'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'F0'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.snmp2-set-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.snmp2-get.html" class="function" rel="rdfs-seeAlso">snmp2_get()</a> - Fetch an SNMP object</span></li>
   </ul>
  </p>
 </div>

 
</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.snmp2-real-walk.html">snmp2_real_walk</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.snmp2-walk.html">snmp2_walk</a></div>
 <div class="up"><a href="ref.snmp.html">SNMP 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
